import { Component, ViewChild, HostListener, ElementRef } from '@angular/core';
import { ActivatedRoute, Route } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { HttpClient } from '@angular/common/http';
import { CompanyService } from '../../company.service';
import { CompanyMenuComponent } from '../companyMenu/companyMenu.component';

/**
 * Company research wrapper component. Adds vertical menu for company research products
 *
 * Author: Sorin Spiru
 * Date: 04.03.2018
 */
@Component({
    templateUrl: './companyResearchWrapper.component.html',
    styleUrls: ['../../company.component.scss']
})
export class CompanyResearchWrapperComponent {
    public routes: Route[];
    public showScrollToTop: boolean = false; 

    @ViewChild('companyMenu') companyMenu: CompanyMenuComponent;
    @ViewChild('menuContainer') menuContainer: ElementRef;

    @HostListener('window:scroll', [])
    onWindowScroll() {
        if (window.pageYOffset > 135) {
            this.menuContainer.nativeElement.classList.add('sticky');
        } else {
            this.menuContainer.nativeElement.classList.remove('sticky');
        }
    }

    constructor(
        private route: ActivatedRoute,
        private companyService: CompanyService) {
        this.routes = this.route.routeConfig.children;
    }

    ngOnInit() {
        //reset expanded paths
        this.companyService.company.subscribe(company => {
            this.companyService.menuItems.next(this.companyService.enableMenuRoutes(this.routes, company));
            this.companyMenu.paths = {};
        });
    }
}